/**
 * channel分页
 */
$(function () {
	_tableshow();
});
function _tableshow() {
	var currentPage = 1;//起始页
	var size = 2;//每页显示条数
    $.ajax({
    	url: "../channel/getChannelList.do",
    	datatype: 'json',
    	type: "Post",
    	data: {
    		page:currentPage,
    		size:size
    	},
    	success: function (data) {
    		if (data != null) {
    			var html;
       			$.each(eval("(" + data + ")").list, function (index, item) { //遍历返回的json
       				html = html + "<tr>";
       				html = html + "<td class='center' id='cid'>"+item.cid+"</td>";
       				html = html + "<td class='hidden-480 center'><img id='logo' style='width:45px;height:45px;' src='"+item.logo+"'/>"+"</td>";
       				html = html + "<td class='center'>"+item.name+"</td>";
       				html = html + "<td class='center'>"+item.labelName+"</td>";
       				html = html + "<td class='center'>"+item.typeName+"</td>";
       				html = html + "<td class='center'>"+item.description+"</td>";
       				html = html + "<td class='center'><span class='label arrowed label-important'>"+item.checkName+"</span></td>";
       				html = html + "<td class='center'><span class='label arrowed label-primary'><input id='status'  onclick='channelList_status($(this));' type='button' value='"+item.statusName+"' style='border: none;background-color: #2283c5;font-size: 12px;line-height: 12px;color: #fff;' /></span></td>";
       				html = html + "<td><div class='center'>";
					html = html + "<a href='javascript:;' class='blue tooltip-info no-hover-underline' data-rel='tooltip' data-original-title='预览频道' onclick='getPreview($(this));'>";
					html = html + "<i class='icon-eye-open bigger-130'></i></a>";
       				html = html + "<a href='javascript:;' class='blue tooltip-info no-hover-underline' data-rel='tooltip' data-original-title='编辑频道' onclick='modifychannel($(this));'>";
       				html = html + "<i class='icon-pencil bigger-130'></i></a>";
       				html = html + "</div></td>";
       				html = html + "</tr>";
       			});
       			$("#list").html(html);
    		var pageCount = eval("(" + data + ")").pageCount; // 取到pageCount的值(把返回数据转成object类型)
    		var currentPage = eval("(" + data + ")").CurrentPage; // 得到CurrentPage
    		var options = {
    				currentPage: currentPage, // 当前页数
    				totalPages: pageCount, // 总页数
    				itemTexts: function (type, page, current) {
    					switch (type) {
    					case "first":
    						return "首页";
    					case "prev":
    						return "上一页";
    					case "next":
    						return "下一页";
    					case "last":
    						return "末页";
    					case "page":
    						return page;
    					}
    				},// 点击事件，用于通过Ajax来刷新整个list列表
    				onPageClicked: function (event, originalEvent, type, page) {
    					$.ajax({
    						url: "../channel/getChannelList.do?",
    						type: "Post",
    						data: {
    				    		page:page,
    				    		size:size
    				    	},
    						success: function (data1) {
    							if (data1 != null) {
    								var html;
    								$.each(eval("(" + data1 + ")").list, function (index, item) { // 遍历返回的json
    									html = html + "<tr>";
    									html = html + "<td class='center' id='cid'>"+item.cid+"</td>";
    									html = html + "<td class='hidden-480 center'><img id='logo' style='width:45px;height:45px;' src='"+item.logo+"'/>"+"</td>";
    				       				html = html + "<td class='center'>"+item.name+"</td>";
    				       				html = html + "<td class='center'>"+item.labelName+"</td>";
    				       				html = html + "<td class='center'>"+item.typeName+"</td>";
    				       				html = html + "<td class='center'>"+item.description+"</td>";
    				       				html = html + "<td class='center'><span class='label arrowed label-important'>"+item.checkName+"</span></td>";
    				       				html = html + "<td class='center'><span class='label arrowed label-primary'><input id='status'  onclick='channelList_status($(this));' type='button' value='"+item.statusName+"' style='border: none;background-color: #2283c5;font-size: 12px;line-height: 12px;color: #fff;' /></span></td>";
    				       				html = html + "<td><div class='center'>";
    				       				html = html + "<a href='javascript:;' class='blue tooltip-info no-hover-underline' data-rel='tooltip' data-original-title='预览频道' onclick='getPreview($(this));'>";
    									html = html + "<i class='icon-eye-open bigger-130'></i></a>";
    				       				html = html + "<a href='javascript:;' class='blue tooltip-info no-hover-underline' data-rel='tooltip' data-original-title='编辑频道' onclick='modifychannel($(this));'>";
    				       				html = html + "<i class='icon-pencil bigger-130'></i></a>";
    				       				html = html + "</div></td>";
    				       				html = html + "</tr>";								
    				       			});
    								$("#list").html(html);
    							}
    						}
    					});
    				}
    			};
          $('#channel_table').bootstrapPaginator(options);
        }
      }
    });
}
function modifychannel(_this){
	var chi = _this.parent().parent().parent().children('td')[7];
	var state=$(chi).children('span').children('input').val();
	var cid = _this.parent().parent().parent().children('td')[0].innerHTML;
	if(state=="关闭"){
		window.location.href="../redirect/channel_edit.do?cid="+cid;
		getChannel(cid);
	}
	else{
		alert("该频道状态下不可编辑！");
	}
}
function getPreview(_this){
	var cid = _this.parent().parent().parent().children('td')[0].innerHTML;
	window.location.href="../redirect/channel_preview.do?cid="+cid;
}
// 频道列表_频道状态切换
function channelList_status(_this){
	var chi = _this.parent().parent().parent().children('td')[7];
	var s=$(chi).children('span').children('input');
	var cid = _this.parent().parent().parent().children('td')[0].innerHTML;
	if(window.confirm('您将要改变频道状态，请谨慎操作！')){
		if(s.val()=="开放"){
			s.val("关闭");
			editStatus(cid,1);
		}else if(s.val()=="关闭"){
			s.val("开放");
			editStatus(cid,0);
		}
		return true;
	}else{
		return false;
	}
}
function editStatus(cid,status){
	$.ajax( {
		url: "../channel/editChannelStatus.do",
		data : {
			cid : cid,
			status:status
		},
		dataType : 'json',
		type: "POST"
	});
}
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)
    	 return  unescape(r[2]); 
     return null;
}
function getChannel(cid){
	$.ajax( {
		dataType : 'json',
		type: "POST",
		url: "../channel/getChannel.do?cid="+cid,
		success: function(data){
			$('#img0').attr("src",data.logo);
			$('#id').val(data.status);
			$('#c_id').val(data.cid);
			$('#c_name').val(data.name);
			if(data.type==0){
				$("input[name='type'][value='0']").attr("checked",true);  //根据Value值设置Radio为选中状态
			}else{
				$("input[name='type'][value='1']").attr("checked",true);  //根据Value值设置Radio为选中状态
			}
			$('#c_agency').val(data.agency);
			$('#label').val(data.label);
			if(data.status==0){
				$("input[name='status'][value='0']").attr("checked",true);  //根据Value值设置Radio为选中状态
			}else{
				$("input[name='status'][value='1']").attr("checked",true);  //根据Value值设置Radio为选中状态
			}
			$('#c_description').val(data.description);
			$('#city').val(data.city);
			$('#c_contact').val(data.contact);
			$('#c_phone').val(data.phone);
          }
	});
}